package team.group.exam.action;

import java.util.List;
import java.util.Map;
import java.util.Random;

import team.group.exam.biz.IPaperBiz;
import team.group.exam.biz.IUsersBiz;
import team.group.exam.entity.TbPaper;
import team.group.exam.entity.TbUser;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UsersAction extends ActionSupport{
	//属性
	private IUsersBiz userBiz;
	private List<TbUser> list;
	private TbUser user;
	private int uid;
	private int currentPage;
	private int totalPage;
	private IPaperBiz paperBiz;
	//试卷集合
	private List<TbPaper> paperNameList;
	//试卷id
	private int pid;
	private TbPaper paper;
	private Map<String,Object> session;
	//试卷类型
	private String paperType;
	
	/**
	 * 用户关联已有试卷
	 * @return
	 */
	public String relationExsitPaper(){
		paper = paperBiz.getById(pid);
		//从session范围中获取用户
	    user = (TbUser)session.get("backuser");
		paper.setTbUser(user);
		userBiz.addUserToPaper(paper);
		return SUCCESS;
	}
	/**
	 * 管理员给用户随机组卷
	 */
	public String newUserPaper(){
		user = (TbUser)session.get("backuser");
		System.out.println("action中的user：" + user);
		TbPaper paper2  = new TbPaper();
		//paper2.setPaperName(paper.getPaperName());
		paper2.setPaperName(paper.getPaperName());
		paper2.setPaperTime(paper.getPaperTime());
		if(paper != null && paper.getPaperType() != null){
			paper2.setPaperType(paper.getPaperType());
		}else{
			paper2.setPaperType("自定义");
		}
		//增加试卷
		paperBiz.addPaper(paper2);
		//随机生成试卷，并把试卷和题目加入试卷_题库表
		paperBiz.addRandomPaper(10, paper2);
		//把试卷和用户关联起来
		paper2.setTbUser(user);
		userBiz.addUserToPaper(paper2);
		//得到该用户所关联的试卷
		//paperNameList = userBiz.getUserPaper(user);
		return SUCCESS;
	}
	/**
	 * 查找该用户所关联的试卷集合
	 */
	public String getUserPaper(){
		user = (TbUser)session.get("backuser");
		paperNameList = userBiz.getUserPaper(user);
		return SUCCESS;
	}
	
	/**
	 * 查询所有试卷
	 */
	public String getAllPaper(){
		paperNameList = paperBiz.getPaperName(null);
		return SUCCESS;
	}
	/**
	 * 列出所有的用户
	 * @return
	 */
	public String getAllUser(){
		list = userBiz.getAllUsers();
		return SUCCESS;
	}
	/**
	 * 列出所有的用户
	 * @return
	 */
	public String getAllUser2(){
		if(currentPage <= 0){
			currentPage = 1;
		}
		if(totalPage <= 0){
			totalPage = 1;
		}
		if(currentPage > totalPage){
			currentPage = totalPage;
		}
		list = userBiz.getAllUsers(currentPage);
		totalPage = userBiz.getTotalPage();
		return SUCCESS;
	}
	/**
	 * 根据id查询一条用户
	 * @return
	 */
	public String getById(){
		user = userBiz.getUserById(uid);
		session = ActionContext.getContext().getSession();
		session.put("backuser", user);
		return SUCCESS;
	}
	
	/**
	 * 前台根据id查询一条用户
	 * @return
	 */
	public String getById2(){
		user = (TbUser) session.get("backuser");
		return SUCCESS;
	}
	/**
	 * 删除一个用户
	 * @return
	 */
	public String delete(){
		userBiz.deleteUser(userBiz.getUserById(uid));
		return SUCCESS;
	}
	
	/**
	 * 修改一条用户信息
	 * @return
	 */
	public String update(){
		user = userBiz.getUserById(uid);
		return SUCCESS;
	}
	/**
	 * 增或者修改一条用户记录
	 * @return
	 */
	public String regist(){
		if(user.getUserId() == null){
			userBiz.addUser(user);
		}else{
			userBiz.updateUser(user);
		}
		return SUCCESS;
	}
	/**
	 * 前台用户注册
	 * @return
	 */
	public String regist2(){
		userBiz.addUser(user);
		return SUCCESS;
	}
	/**
	 * 前台用户登录验证方法
	 * @return
	 */
	public String login(){
		boolean bo = userBiz.userLogin(user);
		if(bo){
			TbUser u2 = userBiz.getcheckUser(user);
			//如果登录成功，把该用户保存在session范围中
			session = ActionContext.getContext().getSession();
			session.put("backuser", u2);
			System.out.println("action--u2:" + u2.getUserId());
			//登录成功，查询该用户所有的试卷
			paperNameList = paperBiz.getPaperName(null);
			System.out.println("该用户的试卷：" + paperNameList.size());
			return SUCCESS;
		}
		return ERROR;
	}
	
	
	
	
	
	//getter()和setter()方法
	public IUsersBiz getUserBiz() {
		return userBiz;
	}

	public void setUserBiz(IUsersBiz userBiz) {
		this.userBiz = userBiz;
	}

	public List<TbUser> getList() {
		return list;
	}

	public void setList(List<TbUser> list) {
		this.list = list;
	}

	public TbUser getUser() {
		return user;
	}

	public void setUser(TbUser user) {
		this.user = user;
	}

	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public IPaperBiz getPaperBiz() {
		return paperBiz;
	}
	public void setPaperBiz(IPaperBiz paperBiz) {
		this.paperBiz = paperBiz;
	}
	public List<TbPaper> getPaperNameList() {
		return paperNameList;
	}

	public void setPaperNameList(List<TbPaper> paperNameList) {
		this.paperNameList = paperNameList;
	}

	public TbPaper getPaper() {
		return paper;
	}

	public void setPaper(TbPaper paper) {
		this.paper = paper;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}
	public String getPaperType() {
		return paperType;
	}
	public void setPaperType(String paperType) {
		this.paperType = paperType;
	}	
}
